import React, { FC } from 'react';

import { getPathConfigByPageAlias, PageAlias, RouteBuilder } from '@libs/common/router';
import { Link, LinkProps } from '@libs/elk-ui';
import { isEmpty } from '@libs/common/utils';

interface SimpleLinkProps extends LinkProps {
    pageAlias?: PageAlias;
    pageParams?: { [key: string]: string | number | boolean };
}

const SimpleLink: FC<SimpleLinkProps> = (props) => {
    if (isEmpty(props.pageAlias) && isEmpty(props.href)) {
        throw new Error("pageAlias and href can't empty at same time.");
    }

    const pathConfig = getPathConfigByPageAlias(props.pageAlias);

    const href =
        props.href ||
        new RouteBuilder(pathConfig?.host).setProtocol('http').setPath(pathConfig?.path).build(props.pageParams);

    return (
        <Link {...props} href={href}>
            {props.children}
        </Link>
    );
};

SimpleLink.displayName = 'SimpleLink';

export { SimpleLink, SimpleLinkProps };
